package com.ahmedblog.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.ahmedblog.model.Author;
import com.ahmedblog.model.BlogEntry;

public class DBManager {
	private Connection conn;

	public void connect() {
		try {
			// load the driver
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			String userName = "ahmedblog";
			String password = "ahmedblog";
			String url = "jdbc:mysql://beforemotion.com/ahmedblog";
			this.conn = DriverManager.getConnection(url, userName, password);
			System.out.println("Database connection established");
		} catch (Exception ex) {
			System.out.println("Database connection Failed");
			ex.printStackTrace();
		}
	}

	public void disconnect() {
		try {
			this.conn.close();
		} catch (Exception ex) {
			System.out.println("Database connection close Failed");
			ex.printStackTrace();
		}
	}

	public Author getAuthor(int id1) {
		Author author = null;
		try {
			Statement statementInsert = conn.createStatement();
			ResultSet rs =  statementInsert.executeQuery("select id,name,email from author where id = id1");
			String name = "";
			String email = "";
			if ( rs.next() )
			{
				name = rs.getString("name");
				email = rs.getString("email");
			}
			else
				throw new Exception("Record not found");
			author = new Author(id1 ,name, email);
			statementInsert.close();
		} catch (Exception ex) {
			System.out.println("Get Author Failed");
			ex.printStackTrace();
		}
		return author;
	}

	public boolean createAuthor(Author au) {
		try {
			Statement statementInsert = conn.createStatement();
			statementInsert.execute("insert into author (name, email) values ('" + au.name + "', '" + au.email + "')");
			statementInsert.close();
			return true;
		} catch (Exception ex) {
			System.out.println("Create Author Failed");
			ex.printStackTrace();
			return false;
		}
	}

	public boolean editAuthor(Author au) {
		try {
			Statement statementInsert = conn.createStatement();
			statementInsert.execute("update author set name = au.name and email=au.email where id = au.id");
			statementInsert.close();
			return true;
		} catch (Exception ex) {
			System.out.println("Update Author Failed");
			ex.printStackTrace();
			return false;
		}	
	}

	public boolean deleteAuthor(Author au) {
		try {
			Statement statementInsert = conn.createStatement();
			statementInsert.execute("delete from author where id = au.id");
			statementInsert.close();
			return true;
		} catch (Exception ex) {
			System.out.println("Delete Author Failed");
			ex.printStackTrace();
			return false;
		}
	}

	public BlogEntry getBlogEntry(int id1) {
		BlogEntry blogentry = null;
		try {
			Statement statementInsert = conn.createStatement();
			ResultSet rs =  statementInsert.executeQuery("select id,heading,body,dt_published,id_author from blog_entry where id = id1");
			String heading = "";
			String body = "";
			String dt_published = "";
			String id_author ="";
			if ( rs.next() )
			{
				heading = rs.getString("heading");
				body = rs.getString("body");
				dt_published = rs.getString("dt_published");
				id_author = rs.getString("id_author");
			}
			else
				throw new Exception("Record not found");
			blogentry = new BlogEntry(id1 ,heading, body,dt_published,id_author );
			statementInsert.close();
		} catch (Exception ex) {
			System.out.println("Get Blog Entry Failed");
			ex.printStackTrace();
		}
		return blogentry;
	}

	public boolean createBlogEntry(BlogEntry be) {
		try {
			Statement statementInsert = conn.createStatement();
			statementInsert.execute("insert into author (heading, body,dt_published,id_author) values ('" + be.heading + "', '" + be.body + "', '" + be.dt_published + "', '" + be.id_author + "')");
			statementInsert.close();
			return true;
		} catch (Exception ex) {
			System.out.println("Create Blog Entry Failed");
			ex.printStackTrace();
			return false;
		}
	}

	public boolean editBlogEntry(BlogEntry be) {
		try {
			Statement statementInsert = conn.createStatement();
			statementInsert.execute("update author set heading = be.heading and body=be.body and dt_published=be.dt_published and id_author=be.id_author where id = au.id");
			statementInsert.close();
			return true;
		} catch (Exception ex) {
			System.out.println("Update Blogentry Failed");
			ex.printStackTrace();
			return false;
		}	}

	public boolean deleteBlogEntry(BlogEntry be) {
		try {
			Statement statementInsert = conn.createStatement();
			statementInsert.execute("delete from author where id = be.id");
			statementInsert.close();
			return true;
		} catch (Exception ex) {
			System.out.println("Delete BlogEntry Failed");
			ex.printStackTrace();
			return false;
		}
	}

}
